/**
 * Created by zg
 */
define(['angular', 'app', 'app.services', 'app.directives', 'layer','services/common/base','services/common/user' ], function (angular, app, services, directives, layer) {
    'use strict';


    /**
     *  配置当前APP
     */
    app.config(['$locationProvider', '$httpProvider', function ($locationProvider, $httpProvider) {



        //配置layer 根目录
        layer.config({
            extend: ['extend/layer.ext.js', 'skin/layer.ext.css'],
            path: '/app/lib/layer/'
        });

        //禁用ajax的缓存
        $.ajaxSetup({
            cache: false
        });


        //添加html模板缓存key
        $httpProvider.interceptors.push('ResourceInterceptor');
        //配置路由使用html5Mode
        $locationProvider.html5Mode(true);
    }]);



    /**
     * 指令HTML模板缓存key 管理
     */
    directives.config(['$httpProvider', function ($httpProvider) {

        //注册拦截器
        $httpProvider.interceptors.push('ResourceInterceptor');

    }]);

    /**
     * 服务拦截器
     */
    services.config(['$httpProvider', function ($httpProvider) {
        //注册拦截器
        $httpProvider.interceptors.push('ResourceInterceptor');
    }]);


    /**
     * 程序入口
     */
    services.service('Main', ['$q','$injector','User', function ($q,$injector,User) {
        this.start = function () {


            function getUser(){
                return $q(function(resolve){
                    var currentUser = User.getCurrentUser();
                    $q.when(currentUser).then(function(currentUser){
                        app.constant("CurrentUser", currentUser.data);
                        services.constant("CurrentUser", currentUser.data);
                        resolve();
                    },function(error){
                        app.constant("CurrentUser", error);
                        services.constant("CurrentUser", error);
                        resolve();
                    });
                });
            }

            //获取用户
            var user = getUser();
            //启动页面
            //自动加载相关APP启动文件
            //获取页面基础路径
            var basePath =  $('head base').attr("href");
            console.info(basePath);
            var appPath = '/app/page/' + basePath + "app.js";
            requirejs([appPath], function () {
                angular.bootstrap(angular.element("html"), ["app"]);
            });

        };

    }]);
});